package com.anos.smsfun.utility;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.widget.Toast;

import com.anos.smsfun.R;
import com.anos.smsfun.model.MessageItem;

public class Utils {

	public static LogUtils mLogUtils = LogUtils.getInstance();

	/**
	 * Convert from long to date format
	 * 
	 */
	@SuppressLint("SimpleDateFormat")
	public static String convertLongToDateFormat(long longDate, String format) {
		String dateString = new SimpleDateFormat(format).format(new Date(
				longDate));
		return dateString;
	}

	/**
	 * Chuyển định dạng số phone từ 0xxx || 0xxx xx... sang +84xxx
	 * 
	 */
	public static String converStandardNumber(final String strPhone) {
		if (strPhone == null)
			return null;
		String result = strPhone;
		result = strPhone.trim();
		int i = result.indexOf("0");
		if (i == 0) {
			result = result.substring(1, result.length());
			result = "+84" + result;
		}
		result = result.replace(" ", "");
		return result;
	}

	/**
	 * Function call phone
	 * 
	 */
	public static void callPhone(Context context, String number) {
		try {
			Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ number));
			context.startActivity(callIntent);
		} catch (Exception e) {
			Toast.makeText(context, context.getString(R.string.can_not_call),
					Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * show message dialog
	 * 
	 */
	public static void showMessageDialog(Context context, String msg) {
		Builder builder = new Builder(context);
		builder.setTitle(context.getResources().getString(R.string.app_name));
		builder.setMessage(msg);
		builder.setNegativeButton(
				context.getResources().getString(R.string.ok),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		AlertDialog alertDialog = builder.create();
		alertDialog.show();
	}

	/**
	 * show message dialog info msg
	 * 
	 */
	public static void showDialogInfoMsg(Context context, MessageItem msgItem) {
		String msg = "";
		if (msgItem.getType() == 1) {
			msg = "From: "
					+ msgItem.getNameDisplay()
					+ "["
					+ msgItem.getAddress()
					+ "]"
					+ "\nReceived: "
					+ msgItem
							.getFormatDate(DefConstants.DATE_FORMAT_DDMMYYYYHHMM)
					+ "\nType: SMS";
		} else {
			msg = "To: "
					+ msgItem.getNameDisplay()
					+ "["
					+ msgItem.getAddress()
					+ "]"
					+ "\nSent: "
					+ msgItem
							.getFormatDate(DefConstants.DATE_FORMAT_DDMMYYYYHHMM)
					+ "\nType: SMS";
		}
		Builder builder = new Builder(context);
		builder.setTitle("Detail");
		builder.setMessage(msg);

		AlertDialog alertDialog = builder.create();
		alertDialog.show();
	}

	/**
	 * Setup action bar
	 * 
	 */
	public static void setUpActionBar(Activity activity) {
		if (activity == null)
			return;
		final ActionBar actionBar = activity.getActionBar();
		actionBar.setDisplayShowCustomEnabled(true);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setBackgroundDrawable(
				activity.getResources().getDrawable(
						R.drawable.ab_background_textured_smsfun));
	}

	/**
	 * send text content
	 * 
	 */
	public static void sendTextContent(Context context, String text) {
		Intent sendIntent = new Intent();
		sendIntent.setAction(Intent.ACTION_SEND);
		sendIntent.putExtra(Intent.EXTRA_TEXT, text);
		sendIntent.setType("text/plain");
		context.startActivity(Intent.createChooser(sendIntent, context
				.getResources().getText(R.string.mnu_share)));
	}

	// ///////////////////////////////////////////////////
	// NOTIFICATION
	// ///////////////////////////////////////////////////
	public static void setBadge(Context context, int count) {
		String launcherClassName = getLauncherClassName(context);
		if (launcherClassName == null) {
			return;
		}
		Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
		intent.putExtra("badge_count", count);
		intent.putExtra("badge_count_package_name", context.getPackageName());
		intent.putExtra("badge_count_class_name", launcherClassName);
		context.sendBroadcast(intent);
	}
	
	private static String getLauncherClassName(Context context) {

		PackageManager pm = context.getPackageManager();

		Intent intent = new Intent(Intent.ACTION_MAIN);// ///////////////////////
		intent.addCategory(Intent.CATEGORY_LAUNCHER);

		List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
		for (ResolveInfo resolveInfo : resolveInfos) {
			String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
			if (pkgName.equalsIgnoreCase(context.getPackageName())) {
				String className = resolveInfo.activityInfo.name;
				return className;
			}
		}
		return null;
	}
}
